import React, {Component} from 'react';
import {Provider} from 'react-redux';
import configureStore from './store/ConfigureStore';
import {StyleSheet, Text, View,Platform,BackHandler,BackAndroid,ToastAndroid} from 'react-native';
import App from './container/App';

// app的入口
const store = configureStore();
export default class Root extends Component {
    constructor(){
        super();
        this.lastBackPressed=Date.now();
    }
    componentWillMount() {
        if (Platform.OS === 'android') {
            BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }
    componentWillUnmount() {
        if (Platform.OS === 'android') {
            BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
        }
    }
    onBackAndroid = () => {
        if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
            //最近2秒内按过back键，可以退出应用。
            BackHandler.exitApp();
        }
        this.lastBackPressed = Date.now();
        ToastAndroid.show('再按一次退出应用',ToastAndroid.SHORT);
        return true;
    };
    render() {
        return (
            <Provider store={store}>
                <View style={styles.container}>
                        <App/>
                </View>
            </Provider>
        )
    }

}

const styles = StyleSheet.create({
    container: {
        width:"100%",
        height:"100%"
    },
});
